<?php
require_once 'BaseController.php';

/**
 * 【SP版】物件詳細 コントローラ
 */
class Mobileapi_DetailController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();

        $typ = $params['Type'];
        $code = $params['Code'];
        $map = $params['Map'];

        $this->makeTemplateParams($code);

        if (empty($this->_data['buken']['T03_HEYA_CD']))
        {
            error_log('no bukken');
            $this->my_redirect('sp/404/');

            return;
        }

        if ($map == 'map')
        {
            $this->displayBody2('map');
        }
        else if ($map == 'pic')
        {
            $this->displayBody2('pic');
        }
        else
        {
            $this->displayBody2($typ);
        }
    }

    private function makeTemplateParams($code)
    {
        $model = new Rstdtl_Model_Bukendtl();
        $model_campaign = new Mobileapi_Model_BukkenCampaign();
        $model_shop = new Mobileapi_Model_BukkenTenpo();
        $model_shop_base = new Shop_Model_Base();

        $config = array();
        $params = array();

        $params['ID'] = $code;
        $params['buken_static'] = '1';
        $params['HeyaCd'] = $code;

        $data = $model->getBukenDtlListData($params);
        $model->convertData($config, $data, $params);

        $index = 1;
        foreach ($data[0]['BUKEN_IMAGE'] as $k => $v)
        {
            foreach ($v as $k1 => $v1)
            {
                $data[0]['BUKEN_IMAGE'][$k][$k1]['MODAL_INDEX'] = sprintf("%02d", $index);
                $index++;
            }
        }

        $tenpo1 = $model_shop->getTenpoList($code);
        if (count($tenpo1) == 0)
        {
            $tenpo1 = $model_shop->getTenpoListHeya($code);
        }
        $data[0]['tenpo1'] = $tenpo1;

        $heya_campaigns = $model_campaign->getHeyaCampaigns($code);
        $norm_campaigns = $model_campaign->getNormalCampaigns();
        $modelLst = new Mobileapi_Model_BukkenList();

        $data[0]['campaigns'] = array_merge($heya_campaigns, $norm_campaigns);

        $lineFlg = false;
        // CALLNOTE
        foreach ($data[0]['tenpo1'] as $i => $tenpo)
        {
            $tempData = Apm_ShopUtil::getCallingData($tenpo, 6);
            $data[0]['tenpo1'][$i]['IS_FREE_CALL'] = $tempData['IS_FREE_CALL'];
            $data[0]['tenpo1'][$i]['hasFreeTel'] = $tempData['hasFreeTel'];
            if ($tempData['IS_FREE_CALL'])
            {
                $data[0]['tenpo1'][$i]['FREE_CALLING'] = $tempData['TEL_01'];
                $data[0]['tenpo1'][$i]['CONVERT_CONNECTION_NO'] = $tempData['CONNECTION_NO'];
            }
            // 営業時間変換
            $data[0]['tenpo1'][$i]['EIGYOU_ZIKAN'] = Apm_Util::replaceUnicode($model_shop_base->convertEigyouZikan($data[0]['tenpo1'][$i]));
            // 定休日
            $data[0]['tenpo1'][$i]['M05_TEIKYUUBI_NAME'] = $model_shop_base->convertTeikyuubi($data[0]['tenpo1'][$i]);
            // LINEチェック
            if (!empty($tenpo['M05_TENPO_LINE_ID']))
            {
                $lineFlg = true;
            }
        }

        // PV増分処理用(代表物件の場合も考慮し、取り扱い店舗のみカウント)
        $model->S_RegPageViewLog($data[0]["M05_TENPO_CD"], 0, 95, $data[0]['T03_ORIGINAL_HEYA_CD01'], 1);

        // Callクレヨン対応
        $smodel = new Shop_Model_Detail();
        foreach ($data[0]['tenpo1'] as $i => $tenpo)
        {
            $callCrayonNumber = $smodel->getCallCrayonNumber($this->_config['callcrayons']['api'], $this->_config['callcrayons']['service_id'], $data[0]['tenpo1'][$i]['T03_TENPO_CD'], '12');
            $data[0]['tenpo1'][$i]['CALL_CRAYON_NO'] = $callCrayonNumber;
            /*
             * if (! empty ( $data [0] ['tenpo1'] [$i] ['CALL_CRAYON_NO'] )) { // フリー番号ではない・・・ $data
             * [0] ['tenpo1'] [$i] ['hasFreeTel'] = false; $data [0] ['tenpo1'] [$i]
             * ['IS_FREE_CALL'] = false; }
             */
        }

        $data[0]['line_flg'] = $lineFlg;
        $this->_data['buken'] = $data[0];

        // 同じ建物のほかの物件
        $tatemodel = new Rstdtl_Model_Articledtl();
        $tatemodel_conv = new Rstobi_Model_Converte();

        $tateparams = array();
        $tatemonoCd = $data[0]['T03_DAIHYO_TATEMONO_CD'];
        $tateparams['ID'] = $tatemonoCd;
        $tateparams['buken_static'] = '1';
        $tateparams['TatemonoCd'] = $tatemonoCd;

        $tatemonodata = $tatemodel->getArticleDetail($tatemonoCd, $data[0]['T03_HEYA_CD']);
        if (isset($tatemonodata) && sizeof($tatemonodata) > 0)
        {
            $tatemodel_conv->dataConverteForBuken($tatemonodata, $tateparams, false, false);
            $tatemodel->convertData($tatemonodata, $tateparams);

            for ($i = 0; $i < sizeof($tatemonodata); $i++)
            {
                // キャンペーン取得
                $heyaCd = $tatemonodata[$i]['T03_HEYA_CD'];
                $ret = $modelLst->getCamp($heyaCd);
                if (!empty($ret))
                {
                    $tatemonodata[$i]['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                    $tatemonodata[$i]['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                    $tatemonodata[$i]['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
                }
            }
            $this->_data['bukenlist'] = $tatemonodata;
            // 同じ建物のほかの物件
        }

        // ★★★周辺物件探す
        $armodel = new Rstdtl_Model_Articledtl();
        $shuuhens = array();
        $shuuhenBukken = $armodel->getShuuhenBukken($data[0]);
        foreach ($shuuhenBukken as $d)
        {
            $d['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
            $d['T03_YACHIN_STR'] = Apm_Bukkenitems::GetChinryouStr($d); // 賃料
            $d['T03_KANRI_KYOEKI_STR'] = Apm_Bukkenitems::GetKyouekihiStr($d, '1'); // 管理費・共益費
            $d['T03_SHIKIKIN_STR'] = Apm_Bukkenitems::GetShikikinStr($d); // 敷金
            $d['T03_REIKIN_STR'] = Apm_Bukkenitems::GetReikinStr($d);
            $d['T03_HOSHOUKIN_STR'] = Apm_Bukkenitems::GetHoshoukinStr($d); // 保証金
            $d['T03_SHOUKYAKUKIN_STR'] = Apm_Bukkenitems::GetShoukyakukinStr($d); // 敷引金・償却金
            $d['T03_MADORI_STR'] = Apm_Bukkenitems::GetMadoriStr($d); // 間取り
            $d["CONVERT_T03_HOUGAKU"] = Apm_Bukkenitems::GetHeyamukiStrSimple($d);

            $shuuhens[] = $d;
        }
        $this->_data['shuuhen'] = $shuuhens;
        // ★★★周辺物件探す
    }

    private function getAreaData()
    {
        $code = $this->_data['T03_TODOUFUKEN_CD'];
        $this->_data['PrefName'] = $this->getPrefName($code);
        $this->_data['PrefNameJP'] = $this->getPrefNameJP($code);
    }

}
?>